package com.css.fxfzfxqh.modules.ysx.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzfxqh.base.dict.service.DictItemService;
import com.css.fxfzfxqh.modules.ysx.param.FxqhJzwYSXParam;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class YsxVerifyHandler implements IExcelVerifyHandler<FxqhJzwYSXParam> {

    @Autowired
    DictItemService dictItemService;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(FxqhJzwYSXParam fxqhJzwYSX) {
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");
        if (PlatformObjectUtils.isEmpty(fxqhJzwYSX.getProvince())) {
            result.setSuccess(false);
            message.append("省 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(fxqhJzwYSX.getCity())) {
            result.setSuccess(false);
            message.append("市 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(fxqhJzwYSX.getCounty())) {
            result.setSuccess(false);
            message.append("县 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(fxqhJzwYSX.getStructureType1())) {
            result.setSuccess(false);
            message.append("结构类型 不能为空、");
        }

        if (PlatformObjectUtils.isEmpty(fxqhJzwYSX.getFortify())) {
            result.setSuccess(false);
            message.append("设防情况 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(fxqhJzwYSX.getDamageDegree())) {
            result.setSuccess(false);
            message.append("烈度 不能为空、");
        }
        if("1".equals(fxqhJzwYSX.getYsxType())){
            if (PlatformObjectUtils.isEmpty(fxqhJzwYSX.getYear())) {
                result.setSuccess(false);
                message.append("年代 不能为空、");
            }
            if (PlatformObjectUtils.isEmpty(fxqhJzwYSX.getHousingUse1())||PlatformObjectUtils.isEmpty(fxqhJzwYSX.getHousingUse2())) {
                result.setSuccess(false);
                message.append("用途 不能为空、");
            }
            if (PlatformObjectUtils.isEmpty(fxqhJzwYSX.getNumberOfFloors())) {
                result.setSuccess(false);
                message.append("层数 不能为空、");
            }
        }
        if(PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity1())){
            if(!pattern(fxqhJzwYSX.getIntensity1())){
                result.setSuccess(false);
                message.append("基本完好 需为非负数且小数位不超过两位、");
            }
        }
        if(PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity2())){
            if(!pattern(fxqhJzwYSX.getIntensity2())){
                result.setSuccess(false);
                message.append("轻微破坏sl 需为非负数且小数位不超过两位、");
            }
        }
        if(PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity3())){
            if(!pattern(fxqhJzwYSX.getIntensity3())){
                result.setSuccess(false);
                message.append("中等破坏ml 需为非负数且小数位不超过两位、");
            }
        }
        if(PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity4())){
            if(!pattern(fxqhJzwYSX.getIntensity4())){
                result.setSuccess(false);
                message.append("严重破坏el 需为非负数且小数位不超过两位、");
            }
        }
        if(PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity5())){
            if(!pattern(fxqhJzwYSX.getIntensity5())){
                result.setSuccess(false);
                message.append("倒塌c 需为非负数且小数位不超过两位、");
            }
        }

        if(PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity1())&&PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity2())&&PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity3())
                &&PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity4())&&PlatformObjectUtils.isNotEmpty(fxqhJzwYSX.getIntensity5())){
            BigDecimal value = new BigDecimal(fxqhJzwYSX.getIntensity1()).add(new BigDecimal(fxqhJzwYSX.getIntensity2()))
                    .add(new BigDecimal(fxqhJzwYSX.getIntensity3())).add(new BigDecimal(fxqhJzwYSX.getIntensity4()))
                    .add(new BigDecimal(fxqhJzwYSX.getIntensity5()));
            if(!predict(value)){
                result.setSuccess(false);
                message.append("易损性值填写时，五个破坏状态的值的和不为100、");
            }
        }
        String mess = message.toString();
        if (!PlatformObjectUtils.isEmpty(mess)) {
            mess = mess.substring(0,mess.length()-1)+"。";
        }
        result.setMsg(mess);
        return result;
    }

    public static void main(String[] args) {
        FxqhJzwYSXParam fxqhJzwYSX=new FxqhJzwYSXParam();
        fxqhJzwYSX.setIntensity1("92.12");
        fxqhJzwYSX.setIntensity2("2.12");
        fxqhJzwYSX.setIntensity3("2.13");
        fxqhJzwYSX.setIntensity4("0");
        fxqhJzwYSX.setIntensity5("3.63");
        BigDecimal value = new BigDecimal(fxqhJzwYSX.getIntensity1()).add(new BigDecimal(fxqhJzwYSX.getIntensity2()))
                .add(new BigDecimal(fxqhJzwYSX.getIntensity3())).add(new BigDecimal(fxqhJzwYSX.getIntensity4()))
                .add(new BigDecimal(fxqhJzwYSX.getIntensity5()));

//        boolean pattern = pattern("");
//        System.out.println(pattern);
    }
    //非负数  并且小数位不能超过两位
    private static boolean pattern(String str){
        String pattern = "^\\d+(\\.\\d{1,2})?$";
        return str.matches(pattern);
    }

    private boolean predict(BigDecimal d) {
        if (d.compareTo(new BigDecimal(100)) == 0) {
            return true;
        }
        return false;
    }
}
